package com.sh.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.sh.common.ResponseInfoEnum;
import com.sh.common.ReturnVO;
import com.sh.entity.Stone;
import com.sh.entity.SystemParam;
import com.sh.entity.User;
import com.sh.service.StoneService;
import com.sh.util.DateUtil;
import com.sh.util.ExcelUtil;
import com.sh.util.FileUitl;
import com.sh.util.StrUtil;

/**
 *  石子 相关 controller
 * */
@RestController
@RequestMapping("/api/stone")
public class StoneController extends BaseController{

	@Autowired
	StoneService stoneService;

	@ResponseBody
	@RequestMapping("/listStone")
	public Object listStone(HttpServletRequest req){
		User user = null;
		String sysTime = DateUtil.getSysTime("yyyy-MM-dd hh:mm:ss");
		Map<String,Object> conMap = new HashMap<String,Object>();
		//开始日期
		String stoneStartDate =
				StrUtil.filterNull((String)req.getParameter("stoneStartDate"));
		//结束日期
		String stoneEndDate =
				StrUtil.filterNull((String)req.getParameter("stoneEndDate"));
		//客户名称
		String customerName =
				StrUtil.filterNull((String)req.getParameter("customerName"));
		//车号
		String carNum =
				StrUtil.filterNull((String)req.getParameter("carNum"));
		//车主
		String carOwner =
				StrUtil.filterNull((String)req.getParameter("carOwner"));
		//装货地
		String loadAddr =
				StrUtil.filterNull((String)req.getParameter("loadAddr"));
		//卸货地
		String unloadAddr =
				StrUtil.filterNull((String)req.getParameter("unloadAddr"));

		conMap.put("stoneStartDate", stoneStartDate);
		conMap.put("stoneEndDate", stoneEndDate);
		conMap.put("customerName", customerName);
		conMap.put("carNum", carNum);
		conMap.put("carOwner", carOwner);
		conMap.put("loadAddr", loadAddr);
		conMap.put("unloadAddr", unloadAddr);

		List<Stone> list =
				stoneService.listStone(conMap,user,sysTime);

		return list;

	}

	//上传 excel文件,并且到DB
	@ResponseBody
	@RequestMapping("/uploadStone")
	public ReturnVO uploadStone(MultipartFile stoneFile){

		User user = new User();
		SystemParam rootNode = null;
		try {
			if(!stoneFile.isEmpty()){

				String path = FileUitl.createFile(stoneFile,serverPath);
				List<List<Object>> list =
						ExcelUtil.readExcelFirstSheet(path);


				//检验数据的准确性，并转换为  List<Stone> 格式
				List<Stone> stoneList =
						checkDataAndToBO(list);

				String sysTime = DateUtil.getSysTime("yyyy-MM-dd hh:mm:ss");
				String rntMsg =
						stoneService.saveUploadStone(stoneList,user,sysTime);


			}

		} catch (IOException e) {
			// TODO Auto-generated catch block
			log.error("读取文件错误:",e);
			return failForService("400", ResponseInfoEnum.STONE_UPLOAD_FAILED,e.getMessage());

		} catch (Exception e) {
			// TODO Auto-generated catch block
			log.error("上传文件错误:",e);
			return failForService("400", ResponseInfoEnum.STONE_UPLOAD_FAILED,e.getMessage());

		}
		return succForService(ResponseInfoEnum.STONE_UPLOAD_SUCCESS, null);

	}

	//检验数据的准确性，并转换为  List<Stone> 格式
	private List<Stone> checkDataAndToBO(List<List<Object>> list) {
		// TODO Auto-generated method stub
		List<Stone> rtnList = new ArrayList<Stone>();
		for(int i=0;i<list.size();i++){
			List<Object> listTmp = list.get(i);
			Stone stone = new Stone();
			//时间
			stone.setTransportDate((String)listTmp.get(0));
			//客户
			stone.setCustomerName((String)listTmp.get(1));
			//装货地
			stone.setLoadAddr((String)listTmp.get(2));
			//卸货地
			stone.setUnloadAddr((String)listTmp.get(3));
			//代车号
			stone.setReplaceCarNum((String)listTmp.get(4));
			//车号
			stone.setCarNum((String)listTmp.get(5));
			//货物名称
			stone.setGoodsName((String)listTmp.get(6));
			//运输重量
			stone.setTransportWeight((String)listTmp.get(7));
			//运输单价
			stone.setTransportUnitPrice((String)listTmp.get(8));
			//运输金额
			stone.setTransportAmount((String)listTmp.get(9));
			//入库吨位
			stone.setInWeight((String)listTmp.get(10));
			//客户单价
			stone.setCustomerUnitPrice((String)listTmp.get(11));
			//应收金额
			stone.setReceiveAmount((String)listTmp.get(12));
			//出库吨位
			stone.setOutWeight((String)listTmp.get(13));
			//出库单价
			stone.setOutUnitPrice((String)listTmp.get(14));
			//出库金额
			stone.setOutAmount((String)listTmp.get(15));
			//其他费用
			stone.setOtherPrice((String)listTmp.get(16));
			//回扣
			stone.setRebates((String)listTmp.get(17));
			//车主
			stone.setCarOwner((String)listTmp.get(18));
			//备注
			stone.setRemark((String)listTmp.get(19));

			rtnList.add(stone);
		}

		return rtnList;
	}




}
